
package org.fing.edu.uy.esbadp.core.entity;

import java.io.Serializable;
import java.util.List;

public class KeyValuePair implements Serializable {
	private static final long serialVersionUID = 1L;

	private String mKey, mVal;

	public KeyValuePair(){
		
	}
	
	
	public KeyValuePair(String pKey, String pVal) {
		if(pKey == null || pKey.trim().equals("")) {
			throw new IllegalArgumentException("null or empty 'pKey' arg in call.");
		}
		
		mKey = pKey;
		mVal = pVal;
	}

	public String getKey() {
		return mKey;
	}

	public String getValue() {
		return mVal;
	}
	
	public void setKey(String key) {
		mKey = key;
	}

	public void setValue(String value) {
		mVal = value;
	}

	public String toString() {
		return mVal;
	}

	public String dump() {
		return "KVpair[" + mKey + "=" + mVal + "]";
	}
    
    /**
     * Get the value associated with the specified key from the supplied list of Key Value Pairs.
     * <p/>
     * Returns the value from the first matching key.
     * @param key The key to search for.
     * @param list The list of KeyValuePairs to search.
     * @return The value associated with the supplied key, or null if key not found.
     */
    public static String getValue(String key, List<KeyValuePair> list) {
        if(key == null) {
            throw new IllegalArgumentException("null 'key' arg in call.");
        }
        if(list == null) {
            throw new IllegalArgumentException("null 'list' arg in call.");
        }
        
        for(KeyValuePair kvp : list) {
            if(kvp.mKey.equals(key)) {
                return kvp.mVal;
            }
        }
        
        return null;
    }
    
    /**
     * Get the value associated with the specified key from the supplied list of Key Value Pairs.
     * <p/>
     * Returns the value from the first matching key.
     * @param key The key to search for.
     * @param list The list of KeyValuePairs to search.
     * @param defaultVal The default value to be returned where there's no value available for the specified key. 
     * @return The value associated with the supplied key, or null if key not found.
     */
    public static String getValue(String key, List<KeyValuePair> list, String defaultVal) {
        String value = getValue(key, list);
        
        if(value == null) {
            return defaultVal;
        }
        
        return value;
    }
    
    /**
     * Get the boolean equivalent value associated with the specified key 
     * from the supplied list of Key Value Pairs.
     * <p/>
     * Returns:
     * <ul>
     *  <li><b><code>true</code></b>: If value equals "true" or "yes" or "y" (ignoring case).</li>
     *  <li><b><code>false</code></b>: If value equals "false" or "no" or "n" (ignoring case).</li>
     *  <li><b><i>defaultVal</i></b>: If none of the above hold true.</li>
     * </ul>
     * <p/>
     * Returns the value from the first matching key.
     * @param key The key to search for.
     * @param list The list of KeyValuePairs to search.
     * @param defaultVal The default value to be returned where the above listed conditions do not hold
     * for the associated value, or the value is not specified.
     * @return The boolean equivalent value associated with the specified key according to the above specified
     * rules, otherwise the <b><i>defaultVal</i></b> is returned.
     */
    public static boolean getBooleanValue(String key, List<KeyValuePair> list, boolean defaultVal) {
        String value = getValue(key, list);
        
        if(value == null) {
            return defaultVal;
        }
        
        if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("y") || value.equalsIgnoreCase("yes")) {
            return true;
        } else if(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("n") || value.equalsIgnoreCase("no")) {
            return false;
        } else {
            return defaultVal;
        }
    }

    /**
     * Get the numeric <code>double</code> equivalent value associated with the specified key 
     * from the supplied list of Key Value Pairs.
     * <p/>
     * Returns the value from the first matching key.
     * @param key The key to search for.
     * @param list The list of KeyValuePairs to search.
     * @param defaultVal The default value to be returned where the value is not found or is non-numeric.
     * @return The <code>double</code> equivalent value associated with the specified key if the value is found
     * and is numeric, otherwise the <b><i>defaultVal</i></b> is returned.
     */
    public static double getDoubleValue(String key, List<KeyValuePair> list, double defaultVal) {
        String value = getValue(key, list);
        
        if(value == null) {
            return defaultVal;
        }

        try {
            return Double.parseDouble(value);
        } catch(NumberFormatException e) {
            // return the default...
        }
        
        return defaultVal;
    }
}
